/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 游戏服gm命令
	/// module : gameCmd
	/// </summary>
	public interface SNetGameCmd : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.gameCmd_exec, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.gameCmd_exec, "Recv->" + data.ToString());
#endif
		                ExecRequest _data = (ExecRequest)data;
						return CS_exec(session, _data.cmd, _data.args);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.gameCmd_exec, ()=> new ExecRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        ///  执行命令
        /// </summary>
	    protected ExecResult CS_exec(ISession session, string cmd, string args);
#endregion
#region Client-side Interface
		/// <summary>
        ///  result可能需要输出
        /// </summary>
        public ExecResult SC_execResult(Task<ExecResult> future){return new ExecResult(future);}
        public ExecResult SC_execResult(int code, string result){return new ExecResult(code, result);}
        public ExecResult SC_execResult(int code){return new ExecResult(code);}
#endregion

#region Messages
	public class ExecResult : MessageResult {
	    private string result;
	    public ExecResult(Task<ExecResult> future): base(future) { }
		public ExecResult(int code): base(code) { }
		public new ExecResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal ExecResult(int code, string result){
	        this.code = code;
	        this.result = result;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<ExecResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.gameCmd_execResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.gameCmd_execResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_string(result);
			return si_p;
		}
		public override string ToString() {
    		return "GameCmd.SC_execResult { "
			+ "code="
			+ code
			+ ", result="
			+ result
			+" }";
		}
	}
	private class ExecRequest : IReceiveMessage
    {
		internal string cmd;
		internal string args;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            cmd = _in.HasRemaining() ? _in.Read_string() : default;
            args = _in.HasRemaining() ? _in.Read_string() : default;
		}
        public int GetMessageId() { return NetMessageId.gameCmd_exec; }
        public override string ToString()
        {
            return "GameCmd.exec"+ ", cmd="+ cmd+ ", args="+ args;
        }
    }
	}
#endregion
}